import { sessionStore } from 'store/dist/store.legacy'
const state = {
  indexJinde: [],
  indexMall: [],
  indexBook: []
}

const mutations = {
  setIndexJinde (state, param) {
    sessionStore.set('third_party_jinde', param.data)
    state.indexJinde = param.data
  },
  setIndexMall (state, param) {
    sessionStore.set('third_party_mall', param.data)
    state.indexMall = param.data
  },
  setIndexBook (state, param) {
    sessionStore.set('third_party_book', param.data)
    state.indexBook = param.data
  }
}

const actions = {
  setIndexJinde ({ state, commit }, param) {
    let list = state.indexJinde
    if (list.length == 0) {
      list = sessionStore.get('third_party_jinde') || []
    }

    if (list.length == 0) {
      let url = process.env.VUE_APP_thirdPartyJinde_url
      window.axios(url).then(ret => {
        commit('setIndexJinde', { data: ret.data })
      })
    } else {
      commit('setIndexJinde', { data: list })
    }
  },
  setIndexMall ({ state, commit }, param) {
    let list = state.indexMall
    if (list.length == 0) {
      list = sessionStore.get('third_party_mall') || []
    }
    if (list.length == 0) {
      let url = process.env.VUE_APP_thirdPartyMall_url
      window.axios(url).then(ret => {
        commit('setIndexMall', { data: ret.data })
      })
    } else {
      commit('setIndexMall', { data: list })
    }
  },
  setIndexBook ({ state, commit }, param) {
    let list = state.indexBook
    if (list.length == 0) {
      list = sessionStore.get('third_party_book') || []
    }
    if (list.length == 0) {
      let url = process.env.VUE_APP_thirdPartyMall_url
      window.axios(url).then(ret => {
        commit('setIndexBook', { data: ret.data })
      })
    } else {
      commit('setIndexBook', { data: list })
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}